﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GLImp;

namespace CastleOfTheDisillusioned {
	class CollisionSequence {
		List<BoundingBox> Collisions = new List<BoundingBox>();
		double CollisionFrame = 0;
		public double Speed = 1;

		public List<Entity> CollidedWith = new List<Entity>();

		BoundingBox CurrentCollision {
			get {
				return Collisions[(int)Math.Floor(CollisionFrame)];
			}
		}

		public void Add(BoundingBox Collision, int Coppies = 1) {
			for (int i = 0; i < Coppies; i++) {
				Collisions.Add(Collision);
			}
			CollisionFrame = Collisions.Count;
		}

		public void Add(IEnumerable<BoundingBox> CollisionCollection) {
			foreach (BoundingBox r in CollisionCollection) {
				Add(r);
			}
		}

		public void Draw(double X, double Y, Direction Dir) {
			if (IsRunning()) {
				BoundingBox Current = GetCollision(X, Y, Dir);
				GraphicsManager.DrawRectangle(Current.X, Current.Y, Current.Width, Current.Height, Color.Red);
			}
		}

		public BoundingBox GetCollision(double X, double Y, Direction Dir) {
			BoundingBox Ret = new BoundingBox(CurrentCollision.X, CurrentCollision.Y, CurrentCollision.Width, CurrentCollision.Height);

			if (Dir == Direction.Left) {
				Ret.X = -Ret.Right;
			}

			Ret.X += X;
			Ret.Y += Y;

			return Ret;
		}

		public void Update(double X, double Y, Direction Dir) {
			CollisionFrame += Speed;
		}

		public bool IsRunning() {
			return CollisionFrame < Collisions.Count;
		}

		public void Play() {
			CollisionFrame = 0;
			CollidedWith.Clear();
		}
	}
}
